import 'package:easy_chat_app/commons/db/repository/LocalConversationRepository.dart';
import 'package:easy_chat_app/commons/model/AppUserDetail.dart';
import 'package:easy_chat_app/commons/model/GetChatGroupDetailResultData.dart';
import 'package:easy_chat_app/commons/model/GetContactsUserInfoResultData.dart';
import 'package:easy_chat_app/commons/model/GetFriendRequestListResultData.dart';
import 'package:easy_chat_app/commons/model/GetUserPublicInfoByCodeResultData.dart';
import 'package:easy_chat_app/mobile/page/MobileAddFriendPage.dart';
import 'package:easy_chat_app/mobile/page/MobileChatGroupInfoPage.dart';
import 'package:easy_chat_app/mobile/page/MobileChatGroupUserListPage.dart';
import 'package:easy_chat_app/mobile/page/MobileChatPage.dart';
import 'package:easy_chat_app/mobile/page/MobileContactsInfoPage.dart';
import 'package:easy_chat_app/mobile/page/MobileContactsInfoSettingPage.dart';
import 'package:easy_chat_app/mobile/page/MobileCreateChatGroupPage.dart';
import 'package:easy_chat_app/mobile/page/MobileFriendRequestDetailPage.dart';
import 'package:easy_chat_app/mobile/page/MobileFriendRequestListPage.dart';
import 'package:easy_chat_app/mobile/page/MobileHomePage.dart';
import 'package:easy_chat_app/mobile/page/MobileLoginPage.dart';
import 'package:easy_chat_app/mobile/page/MobileNicknameSettingPage.dart';
import 'package:easy_chat_app/mobile/page/MobileQRCodeScanPage.dart';
import 'package:easy_chat_app/mobile/page/MobileRegisterPage.dart';
import 'package:easy_chat_app/mobile/page/MobileUserChatGroupListPage.dart';
import 'package:easy_chat_app/mobile/page/MobileUserHeadPortraitPreviewPage.dart';
import 'package:easy_chat_app/mobile/page/MobileUserInfoPage.dart';
import 'package:easy_chat_app/mobile/page/MobileUserPublicInfoPage.dart';
import 'package:easy_chat_app/mobile/page/MobileUserQRCodePage.dart';
import 'package:easy_chat_app/mobile/page/MobileUserSettingPage.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:logger/logger.dart';

import '../../mobile/page/MobileUserHeadPortraitPage.dart';

typedef MaterialPageRouteBuilder = MaterialPageRoute Function(
    RouteSettings settings);

class RouteUtils {
  static Logger logger = Logger();

  static final GlobalKey<NavigatorState> navigatorKey = GlobalKey();

  static Map<String, MaterialPageRouteBuilder> routeMap = {
    "/mobileHomePage": (RouteSettings settings) {
      return MaterialPageRoute(
          settings: settings,
          builder: (builder) {
            return MobileHomePage();
          });
    },
    "/mobileLoginPage": (RouteSettings settings) {
      return MaterialPageRoute(
          settings: settings,
          builder: (builder) {
            return const MobileLoginPage();
          });
    },
    "/mobileRegisterPage": (RouteSettings settings) {
      return MaterialPageRoute(
          settings: settings,
          builder: (builder) {
            return const MobileRegisterPage();
          });
    },
    "/mobileUserSettingPage": (RouteSettings settings) {
      return MaterialPageRoute(
          settings: settings,
          builder: (builder) {
            return const MobileUserSettingPage();
          });
    },
    "/mobileUserInfoPage": (RouteSettings settings) {
      return MaterialPageRoute(
          settings: settings,
          builder: (builder) {
            return MobileUserInfoPage();
          });
    },
    "/mobileUserQRCodePage": (RouteSettings settings) {
      return MaterialPageRoute(
          settings: settings,
          builder: (builder) {
            return const MobileUserQRCodePage();
          });
    },
    "/mobileUserHeadPortraitPage": (RouteSettings settings) {
      return MaterialPageRoute(
          settings: settings,
          builder: (builder) {
            return MobileUserHeadPortraitPage();
          });
    },
    "/mobileUserHeadPortraitPreviewPage": (RouteSettings settings) {
      return MaterialPageRoute(
          settings: settings,
          builder: (builder) {
            return const MobileUserHeadPortraitPreviewPage();
          });
    },
    "/MobileChatPage": (RouteSettings settings) {
      return MaterialPageRoute(
          settings: settings,
          builder: (builder) {
            return MobileChatPage();
          });
    },
    "/MobileCreateGroupChatPage": (RouteSettings settings) {
      return MaterialPageRoute(
          settings: settings,
          builder: (builder) {
            return MobileCreateChatGroupPage();
          });
    },
    "/MobileUserChatGroupListPage": (RouteSettings settings) {
      return MaterialPageRoute(
          settings: settings,
          builder: (builder) {
            return MobileUserChatGroupListPage();
          });
    },
    "/MobileNicknameSettingPage": (RouteSettings settings) {
      return MaterialPageRoute(
          settings: settings,
          builder: (builder) {
            return MobileNicknameSettingPage();
          });
    },
    "/MobileChatGroupInfoPage": (RouteSettings settings) {
      return MaterialPageRoute(
          settings: settings,
          builder: (builder) {
            return MobileChatGroupInfoPage();
          });
    },
    "/MobileChatGroupUserListPage": (RouteSettings settings) {
      return MaterialPageRoute(
          settings: settings,
          builder: (builder) {
            return MobileChatGroupUserListPage();
          });
    },
    "/MobileContactsInfoPage": (RouteSettings settings) {
      return MaterialPageRoute(
          settings: settings,
          builder: (builder) {
            return MobileContactsInfoPage();
          });
    },
    "/MobileContactsInfoSettingPage": (RouteSettings settings) {
      return MaterialPageRoute(
          settings: settings,
          builder: (builder) {
            return MobileContactsInfoSettingPage();
          });
    },
    "/MobileAddFriendPage": (RouteSettings settings) {
      return MaterialPageRoute(
          settings: settings,
          builder: (builder) {
            return MobileAddFriendPage();
          });
    },
    "/MobileQRCodeScanPage": (RouteSettings settings) {
      return MaterialPageRoute(
          settings: settings,
          builder: (builder) {
            return MobileQRCodeScanPage();
          });
    },
    "/MobileUserPublicInfoPage": (RouteSettings settings) {
      return MaterialPageRoute(
          settings: settings,
          builder: (builder) {
            return MobileUserPublicInfoPage();
          });
    },
    "/MobileFriendRequestListPage": (RouteSettings settings) {
      return MaterialPageRoute(
          settings: settings,
          builder: (builder) {
            return MobileFriendRequestListPage();
          });
    },
    "/MobileFriendRequestDetailPage": (RouteSettings settings) {
      return MaterialPageRoute(
          settings: settings,
          builder: (builder) {
            return MobileFriendRequestDetailPage();
          });
    }
  };

  static Route<dynamic>? onGenerateRoute(RouteSettings settings) {
    logger.i("name: ${settings.name}, arguments: ${settings.arguments}");
    if (settings.name == null) {
      return null;
    }
    String name = settings.name!;
    // logger.i("routeResult:${routeMap[name]}");
    return routeMap[name]!(settings);
  }

  /// 清空导航栈并跳转到login页面
  static void route2MobileLoginPageAndCleanStack() {
    String currentRouteName = currentRouteSettings().name!;
    if (currentRouteName == "/mobileLoginPage") {
      return;
    }
    navigatorKey.currentState!.pushNamedAndRemoveUntil("/mobileLoginPage",
        (route) {
      return false;
    });
  }

  /// 清空导航栈并跳转到App首页
  static void route2MobileHomePageAndCleanStack() {
    String currentRouteName = currentRouteSettings().name!;
    if (currentRouteName == "/mobileHomePage") {
      return;
    }
    navigatorKey.currentState!.pushNamedAndRemoveUntil("/mobileHomePage",
        (route) {
      return false;
    });
  }

  /// 跳转到用户注册页面
  static void route2MobileRegisterPage() {
    navigatorKey.currentState!.pushNamed("/mobileRegisterPage");
  }

  /// 跳转到用户设置页面
  static void route2MobileUserSettingPage() {
    navigatorKey.currentState!.pushNamed("/mobileUserSettingPage");
  }

  /// 跳转到个人信息页面
  static Future<Object?> route2MobileUserInfoPage() {
    return navigatorKey.currentState!.pushNamed("/mobileUserInfoPage");
  }

  /// 跳转到个人信息页面
  static void route2MobileUserQRCodePage(AppUserDetail appUserDetail) {
    navigatorKey.currentState!
        .pushNamed("/mobileUserQRCodePage", arguments: appUserDetail);
  }

  /// 跳转到头像页面
  static Future<Object?> route2MobileUserHeadPortraitPage(
      AppUserDetail appUserDetail) {
    return navigatorKey.currentState!
        .pushNamed("/mobileUserHeadPortraitPage", arguments: appUserDetail);
  }

  /// 跳转到头像页面
  static Future<Object?> route2MobileUserHeadPortraitPreviewPage(
      AppUserDetail appUserDetail, XFile xFile) async {
    return navigatorKey.currentState!.pushNamed(
        "/mobileUserHeadPortraitPreviewPage",
        arguments:
            MobileUserHeadPortraitPreviewPageParams(xFile, appUserDetail));
  }

  /// 跳转到头像页面
  static Future<Object?> route2MobileChatPage(
      MobileChatPageParams params) async {
    return navigatorKey.currentState!
        .pushNamed("/MobileChatPage", arguments: params);
  }

  /// 跳转到头像页面
  static Future<Object?> route2MobileCreateGroupChatPage() async {
    return navigatorKey.currentState!.pushNamed("/MobileCreateGroupChatPage");
  }

  /// 跳转到聊天群列表页
  static Future<Object?> route2MobileUserChatGroupListPage() async {
    return navigatorKey.currentState!.pushNamed("/MobileUserChatGroupListPage");
  }

  /// 跳转到用户昵称设置页
  static Future<Object?> route2MobileNicknameSettingPage() async {
    return navigatorKey.currentState!.pushNamed("/MobileNicknameSettingPage");
  }

  /// 跳转到添加好友页面
  static Future<Object?> route2MobileAddFriendPage() async {
    return navigatorKey.currentState!.pushNamed("/MobileAddFriendPage");
  }

  /// 跳转到二维码扫描页
  static Future<Object?> route2MobileQRCodeScanPage() async {
    return navigatorKey.currentState!.pushNamed("/MobileQRCodeScanPage");
  }

  /// 跳转到群聊信息页
  static Future<Object?> route2MobileContactsInfoPage(
      GetContactsUserInfoResultData contactsUserInfo,
      LocalConversation localConversation,
      Type prePageType) async {
    return navigatorKey.currentState!.pushNamed("/MobileContactsInfoPage",
        arguments: MobileContactsInfoPageParams.create(
            contactsUserInfo: contactsUserInfo,
            localConversation: localConversation,
            prePageType: prePageType));
  }

  /// 跳转到群聊信息页
  static Future<Object?> route2MobileContactsInfoSettingPage(
      GetContactsUserInfoResultData contactsUserInfo) async {
    return navigatorKey.currentState!.pushNamed(
        "/MobileContactsInfoSettingPage",
        arguments: MobileContactsInfoSettingPageParams.create(
            contactsUserInfo: contactsUserInfo));
  }

  /// 跳转到群聊信息页
  static Future<Object?> route2MobileChatGroupInfoPage(
      {required GetChatGroupDetailResultData chatGroupDetail}) async {
    return navigatorKey.currentState!.pushNamed("/MobileChatGroupInfoPage",
        arguments: MobileChatGroupInfoPageParams.create(
            chatGroupDetail: chatGroupDetail));
  }

  /// 跳转到群聊用户列表页
  static Future<Object?> route2MobileChatGroupUserListPage(
      {required GetChatGroupDetailResultData chatGroupDetail}) async {
    return navigatorKey.currentState!.pushNamed("/MobileChatGroupUserListPage",
        arguments: MobileChatGroupUserListPageParams.create(
            chatGroupDetail: chatGroupDetail));
  }

  /// 跳转到群聊用户列表页
  static Future<Object?> route2MobileUserPublicInfoPage(
      {required GetUserPublicInfoByCodeResultData userPublicInfo}) async {
    return navigatorKey.currentState!.pushNamed("/MobileUserPublicInfoPage",
        arguments: MobileUserPublicInfoPageParams.create(
            userPublicInfo: userPublicInfo));
  }

  /// 跳转到新的朋友列表页
  static Future<Object?> route2MobileFriendRequestListPage(
      List<GetFriendRequestListResultDataFriendRequestView>?
          friendRequestViewList) async {
    return navigatorKey.currentState!.pushNamed("/MobileFriendRequestListPage",
        arguments: MobileFriendRequestListPageParams.create(
            friendRequestViewList: friendRequestViewList));
  }

  /// 跳转到新的朋友列表页
  static Future<Object?> route2MobileFriendRequestDetailPage(
      GetFriendRequestListResultDataFriendRequestView friendRequestView) async {
    return navigatorKey.currentState!.pushNamed(
        "/MobileFriendRequestDetailPage",
        arguments: MobileFriendRequestDetailPageParams.create(
            friendRequestView: friendRequestView));
  }

  static RouteSettings currentRouteSettings() {
    RouteSettings? settings;
    navigatorKey.currentState!.popUntil((route) {
      settings = route.settings;
      return true;
    });
    return settings!;
  }

  static void pop<T extends Object?>([T? result]) {
    navigatorKey.currentState!.pop(result);
  }
}

class MobileUserHeadPortraitPreviewPageParams {
  XFile? xFile;
  AppUserDetail? appUserDetail;

  MobileUserHeadPortraitPreviewPageParams(this.xFile, this.appUserDetail);
}
